package com.ssm.lzh.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.github.pagehelper.PageInfo;
import com.ssm.hj.page.Page;
import com.ssm.lzh.service.PackageService;
import com.ssm.lzh.vo.projectvo;
import com.ssm.pojo.Package;
import com.ssm.pojo.Project;
import com.ssm.pojo.exercise;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.swing.plaf.multi.MultiToolTipUI;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("lzh")
public class PackageController {

    @Resource
    private PackageService packageService;


    @RequestMapping("/uploadImg")
    @ResponseBody
    public String img(MultipartFile file){
// Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
        String endpoint = "oss-cn-hangzhou.aliyuncs.com";
// 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        String accessKeyId =  "LTAI5t8jnn1AXzNU3j9kJH9K";
        String accessKeySecret = "phXj04J1Qm5h0g2VIiFJA4T9zX627L";
// 填写Bucket名称，例如examplebucket。
        String bucketName = "imgag";
//你上传到oss后的名字 会根据日期帮你创建文件夹。
        String objectName = file.getOriginalFilename();
// 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            InputStream inputStream = file.getInputStream();
            // 创建PutObject请求。
            ossClient.putObject(bucketName, objectName, inputStream);
        } catch (Exception oe) {

        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
//https://qy151.oss-cn-hangzhou.aliyuncs.com/2022/6/10/20d3d7e6b5bb455cb548675501f7270fgdnj.jpg
        String url = "https://" + bucketName + "." + endpoint + "/" + objectName;
        return url;

    }


//    @RequestMapping("/uploadImg")
//    @ResponseBody
//    public String uploadImg(MultipartFile file) throws IOException {
//
//        System.out.println(file);
////        String realPath="D:/img";
////        File savefile = new File(realPath);
////        if (!savefile.exists()) {
////            savefile.mkdir();
////        }
////        String oldname = file.getOriginalFilename();
////
////        String newname = UUID.randomUUID() + oldname.substring(oldname.lastIndexOf("."));
////        String savaPath = realPath + "\\" + newname;
//////新图片文件
////        File newfile = new File(savaPath);
//////把上传存在内存中的文件内容写入目标文件中
////        try {
////            file.transferTo(newfile);
////            System.out.println("上传成功");
////            //将文件在服务器的存储路径返回
////            return newname;
////        } catch (IOException e) {
////            System.out.println("上传失败");
////            e.printStackTrace();
////        }
////        return null;
//
//
//
//
//        File  imgPath= new File("D:/img/");
//
//        String originalFilename = file.getOriginalFilename();
//
//        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
//
//
//        String newFileName = UUID.randomUUID().toString().substring(1,9);
//
//
//        if(!imgPath.exists()){
//            imgPath.mkdirs();
//        }
//        System.out.println(imgPath);
//
//        file.transferTo(new File(imgPath+"/"+newFileName+substring));
//          return newFileName+substring;
////
//    }

    //分页查询
    @RequestMapping("/queryAllPackage")
    @ResponseBody
    public PageInfo<Package> queryAllPackage(@RequestBody Page<Package> page){
        return this.packageService.queryAllPackage(page);
    }


    //不分页查询所有
    @RequestMapping("/queryTzAll")
    @ResponseBody
    public List<Package> queryTzAll(){
        return this.packageService.queryTzAll();
    }

    @RequestMapping("/addPackage")
    @ResponseBody
    public int addPackage(@RequestBody projectvo<Package> projectvo){
        return this.packageService.addPackage(projectvo);
    }

    @RequestMapping("/UpdatePackage")
    @ResponseBody
    public int updatePackage(@RequestBody projectvo<Package> projectvo){
        return this.packageService.updatePackage(projectvo);
    }


    @RequestMapping("/queryProByTcId")
    @ResponseBody
    public List<Project> queryProByTcId(int tid){
        return this.packageService.queryProByTcId(tid);
    }
}
